<template>
    <!-- 商品分类导航 -->
    <div class="type-nav">
        <div class="container">
            <div @mouseenter="into" @mouseleave="out">
                <h2 class="all" >全部商品分类</h2>
                <transition name="item">
                    <div class="sort" @click="toSearch" v-show="showOnelevel">
                    <div class="all-sort-list2">
                        <div class="item " :class="{showCategoryList:currentIndex === index}"
                            v-for="(item,index) in categoryList" :key="item.categoryId"
                            @mouseover="showshowCategoryList(index)">
                            <h3>
                                <a href="javascript:;" :data-category-name="item.categoryName"
                                    :data-category1-id="item.categoryId">{{item.categoryName}}</a>
                            </h3>
                            <div class="item-list clearfix">
                                <div class="subitem" v-for="child in item.categoryChild" :key="child.categoryId">
                                    <dl class="fore">
                                        <dt>
                                            <a href="javascript:;" :data-category-name="child.categoryName"
                                                :data-category2-id="child.categoryId">{{child.categoryName}}</a>
                                        </dt>
                                        <dd>
                                            <em v-for="grandChild in child.categoryChild" :key="grandChild.categoryId">
                                                <a href="javascript:;" :data-category-name="grandChild.categoryName"
                                                    :data-category3-id="grandChild.categoryId">{{grandChild.categoryName}}</a>
                                            </em>
                                        </dd>
                                    </dl>
                                </div>
                            </div>
                        </div>
                    </div>
                    </div>
                </transition>
                
            </div>
            <nav class="nav">
                <a href="###">服装城</a>
                <a href="###">美妆馆</a>
                <a href="###">尚品汇超市</a>
                <a href="###">全球购</a>
                <a href="###">闪购</a>
                <a href="###">团购</a>
                <a href="###">有趣</a>
                <a href="###">秒杀</a>
            </nav>

        </div>
    </div>
</template>

<script>
    import {
        mapState
    } from 'vuex'
    import throttle from 'lodash/throttle.js'
    
    export default {
        name: 'TypeNav',
        data() {
            return {
                currentIndex: -2,
                showOnelevel:['/','/home'].includes(this.$route.path)
            }
        },
        computed: {
            ...mapState({
                categoryList(state) {
                    return state.home.categoryList
                }
            }),
        },
        methods: {
           
            //控制一二三级显示与隐藏
            into() {
                this.currentIndex = -1
                if(!['/','/home'].includes(this.$route.path)){
                    this.showOnelevel=true
                }
            },
            out() {
                this.currentIndex = -2
                if(!['/','/home'].includes(this.$route.path)){
                    this.showOnelevel=false
                }
            },
            showshowCategoryList: throttle(function (index) {
                if (this.currentIndex > -2)
                    this.currentIndex = index
            }, 200),
            //三级分类跳转到searc组件
            toSearch(e) {
                const { categoryName = '', category1Id = '', category2Id = '', category3Id = ''} = e.target.dataset
                //加hash,点击相同的路由也能触发$router的改变
                const location = { name: 'search',query: {},hash:`#${Date.now()}` }
                if(Object.keys(this.$route.params).length !== 0){
                    location.params = this.$route.params
                }
                categoryName ? location.query.categoryName = categoryName : ''
                category1Id ? location.query.category1Id = category1Id : ''
                category2Id ? location.query.category2Id = category2Id : ''
                category3Id ? location.query.category3Id = category3Id : ''

                //search组件跳search组件,不需要回退,用replace
                if(this.$route.path.startsWith('/search')){
                    this.$router.replace(location)
                }else{
                    this.$router.push(location)
                }
                //最后隐藏一级分类列表
                this.showOnelevel = false
            }
        }
    }
</script>

<style lang="less" scoped>
    .type-nav {
        border-bottom: 2px solid #e1251b;

        .container {
            width: 1200px;
            margin: 0 auto;
            display: flex;
            position: relative;

            .all {
                width: 210px;
                height: 45px;
                background-color: #e1251b;
                line-height: 45px;
                text-align: center;
                color: #fff;
                font-size: 14px;
                font-weight: bold;
            }

            .nav {
                a {
                    height: 45px;
                    margin: 0 22px;
                    line-height: 45px;
                    font-size: 16px;
                    color: #333;
                }
            }

            .sort {
                position: absolute;
                left: 0;
                top: 45px;
                width: 210px;
                height: 461px;
                position: absolute;
                background: #fafafa;
                z-index: 999;

                .all-sort-list2 {
                    .item {
                        h3 {
                            line-height: 30px;
                            font-size: 14px;
                            font-weight: 400;
                            overflow: hidden;
                            padding: 0 20px;
                            margin: 0;

                            a {
                                color: #333;
                            }
                        }

                        .item-list {
                            display: none;
                            position: absolute;
                            width: 734px;
                            min-height: 460px;
                            _height: 200px;
                            background: #f7f7f7;
                            left: 210px;
                            border: 1px solid #ddd;
                            top: 0;
                            z-index: 9999 !important;

                            .subitem {
                                float: left;
                                width: 650px;
                                padding: 0 4px 0 8px;

                                dl {
                                    border-top: 1px solid #eee;
                                    padding: 6px 0;
                                    overflow: hidden;
                                    zoom: 1;

                                    &.fore {
                                        border-top: 0;
                                    }

                                    dt {
                                        float: left;
                                        width: 54px;
                                        line-height: 22px;
                                        text-align: right;
                                        padding: 3px 6px 0 0;
                                        font-weight: 700;
                                    }

                                    dd {
                                        float: left;
                                        width: 415px;
                                        padding: 3px 0 0;
                                        overflow: hidden;

                                        em {
                                            float: left;
                                            height: 14px;
                                            line-height: 14px;
                                            padding: 0 8px;
                                            margin-top: 5px;
                                            border-left: 1px solid #ccc;
                                        }
                                    }
                                }
                            }
                        }

                        &.showCategoryList {
                            background: #e125ff;

                            .item-list {
                                display: block;
                            }
                        }
                    }
                }
            }
        }
    }
</style>